package com.app.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.app.aspect.Log;
import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.model.ExposureModel;
import com.app.service.AdminUserService;
import com.app.service.ExposureService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
@RequestMapping("/jtwq")
public class AdminExposureController {
    @Autowired
    private ExposureService exposureService;
    @Autowired
    private AdminUserService adminUserService;
    //列表
    @GetMapping("/allBack")
    public Result<Object> getAll(int page, int limit, String exposureTitle, Integer type, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        ExposureModel model = new ExposureModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setExposureTitle(exposureTitle);
        model.setType(type);
        return Result.success(exposureService.List(model));
    }

    /*
   曝光台后台管理
    */
    //添加通知公告
    @PostMapping("/add")
    @Log("添加通知公告")
    public Result<Object> addExposure(@RequestBody JSONObject json,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        String title = (String) json.get("exposureTitle");
        String content = (String) json.get("exposureContent");
        Integer isHeat = (Integer) json.get("isHeat");
        Integer sort = (Integer) json.get("sort");
        Integer type = (Integer) json.get("type");
        Integer id = (Integer) json.get("id");
        return Result.success(exposureService.add(title, content, sort, type));
    }

    //是否热门  /exposure/heat
    @GetMapping("/heat")
    public Result<Object> heat(int id, int isHeat,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        exposureService.selectById(id, isHeat);
        return Result.success("修改成功");
    }

    //编辑通知公告
    @PostMapping("/update")
    @Log("编辑通知公告")
    public Result<Object> updateExposure(@RequestBody JSONObject json,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        String title = (String) json.get("exposureTitle");
        String content = (String) json.get("exposureContent");
        Integer isHeat = (Integer) json.get("isHeat");
        Integer sort = (Integer) json.get("sort");
        Integer id = (Integer) json.get("id");
        Integer type = (Integer) json.get("type");
        return Result.success(exposureService.update(title, content, sort, id, type));
    }

    //删除通知公告
    @GetMapping("/delete")
    @Log("删除通知公告")
    public Result<Object> deleteExposureById(int id,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        exposureService.deleteExposureById(id);
        return Result.success("操作成功！");
    }

    //曝光台分类查询
    @GetMapping("/type")
    public Result<Object> selectType(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(exposureService.selectType());
    }
}
